package com.core.website.service.impl;

import com.core.website.entity.EditPass;
import com.core.website.entity.User;
import com.core.website.mapper.RoleMapper;
import com.core.website.mapper.UserMapper;
import com.core.website.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public User findByUserName(String userName) {
        User byUserName = userMapper.findByUserName(userName);
        return byUserName;
    }
    @Override
    public List<User> queryAll(User user) {
        List<User> userList = userMapper.queryAll(user);
        return userList;
    }
    @Override
    public User queryInfo(String id) {
        User user = userMapper.findById(id);
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(User user) {
        user.setId(UUID.randomUUID().toString().toUpperCase().replace("-", ""));
        ByteSource salt = ByteSource.Util.bytes(user.getUserName());
        /*
         * MD5加密：
         * 使用SimpleHash类对原始密码进行加密。
         * 第一个参数代表使用MD5方式加密
         * 第二个参数为原始密码
         * 第三个参数为盐值，即用户名
         * 第四个参数为加密次数
         * 最后用toHex()方法将加密后的密码转成String
         * */
        String newPs = new SimpleHash("MD5", user.getPassword(), salt.toString(), 1024).toHex();
        user.setPassword(newPs);
        user.setSalt(salt.toString());
        List<String> roleList=new ArrayList<>();
        roleList.add(user.getRoleCode());
        roleMapper.saveUserRole(user.getId(),roleList);
        return userMapper.save(user);
    }
    @Override
    public int edit(User user) {
        return userMapper.edit(user);
    }
    @Override
    public int editPass(EditPass editPass) {
        User subject = (User) SecurityUtils.getSubject().getPrincipal();
        User byId = userMapper.findById(subject.getId());
        String oldPs = new SimpleHash("MD5", editPass.getOldPass(), byId.getSalt(), 1024).toHex();
        if(!oldPs.equalsIgnoreCase(byId.getPassword())){
            return -1;
        }
        String newPs = new SimpleHash("MD5", editPass.getNewPass(), byId.getSalt(), 1024).toHex();
        User user = new User();
        user.setId(subject.getId());
        user.setPassword(newPs);
        return userMapper.editPass(user);

    }
    @Override
    public int editPassById(EditPass editPass) {
        User byId = userMapper.findById(editPass.getId());
        String newPs = new SimpleHash("MD5", editPass.getNewPass(), byId.getSalt(), 1024).toHex();
        User user = new User();
        user.setId(editPass.getId());
        user.setPassword(newPs);
        return userMapper.editPass(user);

    }

    @Override
    public int delete(List<String> ids) {
        if(ids.size()>0){
            roleMapper.deleteByUserIds(ids);
            return userMapper.delete(ids);
        }else{
            return 0;
        }

    }
}
